package com.csglxt.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csglxt.base.DaoSupportImpl;
import com.csglxt.domain.Goods;
import com.csglxt.domain.Market;
import com.csglxt.domain.OnshelvesInfo;
import com.csglxt.domain.Order;
import com.csglxt.domain.OrderItem;
import com.csglxt.service.OnshelvesInfoService;

/**
 * 
 * @ClassName: OnshelvesInfoServiceImpl
 * @Description: TODO(上架管理 业务层 实现类)
 * @author Itchbeilef
 * @date 2015-5-28 下午10:13:56
 * 
 */

@Service
@Transactional
public class OnshelvesInfoServiceImpl extends DaoSupportImpl<OnshelvesInfo>
        implements OnshelvesInfoService {
    /**
     * 
     * @Title: queryStorageByGoods
     * @Description: TODO(跟据商品 超市 查询上架表信息)
     * @param goods
     *            ：商品 market:超市
     * @return
     * @throws
     */
    @SuppressWarnings("unchecked")
    private List<OnshelvesInfo> queryStorageByGoods(Goods goods, Market market) {
        return getSession()
                .createQuery(
                        "FROM OnshelvesInfo o WHERE o.goods = ? and o.market = ?")//
                .setParameter(0, goods)//
                .setParameter(1, market).list();
    }

    /**
     * 
     * @Title: UpGoodsNumbyOrderItem
     * @Description: TODO(跟据单据明细信息增加或者减少相对应商品在上架信息表里的数量,并根据商品所在的超市来操作)
     * @param item
     *            ：单据明细项 jia：增加或减少 market：超市
     * @return
     * @throws
     */
    public int UpGoodsNumbyOrderItem(OrderItem item, boolean jia, Market market) {
        int result = 0;
        if (jia) {
            if (queryStorageByGoods(item.getGoods(), market).size() <= 0) {
                OnshelvesInfo onshelves = new OnshelvesInfo();
                onshelves.setGoods(item.getGoods());
                onshelves.setMarket(market);
                onshelves.setGoodsNumber(item.getNumber());
                this.save(onshelves);
            } else {
                result = getSession()
                        .createQuery(
                                "UPDATE OnshelvesInfo o SET o.goodsNumber=o.goodsNumber+? WHERE o.goods.id=? and o.market.id=?")
                        .setParameter(0, item.getNumber())
                        .setParameter(1, item.getGoods().getId())
                        .setParameter(2, market.getId()).executeUpdate();
            }
        } else {
            result = getSession()
                    .createQuery(
                            "UPDATE OnshelvesInfo o SET o.goodsNumber=o.goodsNumber-? WHERE o.goods.id=? and o.market.id=?")
                    .setParameter(0, item.getNumber())
                    .setParameter(1, item.getGoods().getId())
                    .setParameter(2, market.getId()).executeUpdate();
        }
        return result;
    }

    /**
     * 
     * @Title: UpGoodsNumbyOrderItems
     * @Description: TODO(跟据单据信息增加或减少上架表对应商品数量)
     * @param order
     *            ：单据信息 jia：增加或减少
     * @return
     * @throws
     */
    public int UpGoodsNumbyOrderItems(Order order, boolean jia) {
        int result = 0;
        for (OrderItem item : order.getOrderItems()) {
            result = UpGoodsNumbyOrderItem(item, jia, order.getMarket());
        }
        return result;
    }

    /**
     * 
     * @Title: queryGoodsNumbygoodsid
     * @Description: TODO(根据 商品ID 超市ID 来查询 上架商品数)
     * @param Goodsid
     *            商品ID marketid 超市ID
     * @return
     * @throws
     */
    public int queryGoodsNumbygoodsid(String Goodsid, String marketid) {
        Number result = (Number) getSession()
                .createQuery(
                        "SELECT o.goodsNumber From OnshelvesInfo o  WHERE o.goods.id=? and o.market.id=?")
                .setParameter(0, Goodsid).setParameter(1, marketid)
                .uniqueResult();
        return result.intValue();
    }

}
